package com.gitee.wsl.collections.iterator


/**
 * Returns a view containing the result of applying `function` to each element of `fromIterator`.
 *
 *
 * The returned iterator supports `remove()` if `fromIterator` does. After a
 * successful `remove()` call, `fromIterator` no longer contains the corresponding
 * element.
 */
fun < F: Any, T> Iterator<F>.transform(
     function: (F)->T
): Iterator<T> {
    return object : TransformedIterator<F, T>(this) {
        override fun transform(from: F): T {
            return function(from)
        }
    }
}

internal abstract class TransformedIterator<F , T >(val backingIterator: Iterator<F>) :  Iterator<T> {

    abstract fun transform( from: F): T
    override fun hasNext(): Boolean {
        return backingIterator.hasNext()
    }

    override fun next(): T {
        return transform(backingIterator.next())
    }

    /* override fun remove() {
         backingIterator.remove()
     }*/
}